import bisect
class Solution(object):
    def kthSmallestSubarraySum(self, nums, k):
        n = len(nums)
        for i in range(1, n):
            nums[i] += nums[i - 1]
        def cal(val):
            ans = 0
            for i in range(n):
                if nums[i] <= val:
                    ans += (i + 1)
                else:
                    index = bisect.bisect_left(nums, nums[i] - val, 0, i)
                    ans += i - index
            return ans >= k

        left = 1
        right = sum(nums)
        while left < right:
            mid = (left + right) // 2
            if cal(mid):
                right = mid
            else:
                left = mid + 1
        return left

